import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:oramemo/common/langs/text_key.dart';
import 'package:oramemo/widget/my_refresh.dart';

import 'event_logic.dart';

/// 事件卡片组件
class EventCard extends StatelessWidget {
  final String title;
  final String startTime;
  final String taskDetail;
  final bool isCompleted;
  final VoidCallback? onTap;
  final VoidCallback? onDelete;

  const EventCard({
    Key? key,
    required this.title,
    required this.startTime,
    required this.taskDetail,
    this.isCompleted = false,
    this.onTap,
    this.onDelete,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        height: 89,
        margin: const EdgeInsets.only(bottom: 12),
        decoration: BoxDecoration(
          color: const Color(0xFFEAEEF2),
          borderRadius: BorderRadius.circular(14),
        ),
        child: Row(
          children: [
            const SizedBox(width: 15),
            // 提示图标
            Container(
              width: 43,
              height: 43,
              decoration: const BoxDecoration(
                color: Colors.white,
                shape: BoxShape.circle,
              ),
              child: const Center(
                child: Text(
                  '提示\n闹铃',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 10,
                    fontFamily: 'Allerta',
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ),
            ),
            const SizedBox(width: 16),
            // 内容区域
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    title,
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 16,
                      fontFamily: 'Allerta',
                      fontWeight: FontWeight.w400,
                      decoration:
                          isCompleted ? TextDecoration.lineThrough : null,
                    ),
                  ),
                  const SizedBox(height: 8),
                  Text(
                    '$startTime\n$taskDetail',
                    style: TextStyle(
                      color: const Color(0xFF999999),
                      fontSize: 12,
                      fontFamily: 'Allerta',
                      fontWeight: FontWeight.w400,
                      decoration:
                          isCompleted ? TextDecoration.lineThrough : null,
                    ),
                  ),
                ],
              ),
            ),
            // 状态指示点
            Container(
              width: 11,
              height: 11,
              margin: const EdgeInsets.only(right: 16),
              decoration: BoxDecoration(
                color: isCompleted ? Colors.green : Colors.white,
                shape: BoxShape.circle,
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class EventPage extends StatelessWidget {
  const EventPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final logic = Get.put(EventLogic());

    return Obx(
      () => MyRefresh(
        onRefresh: () {
          logic.onLoad();
        },
        onLoad: () {
          logic.requestData(page: 1);
        },
        child: (logic.events.isEmpty)
            ? ListView(
                children: const [
                  Padding(padding: EdgeInsets.only(top: 100),
                  child: Center(child: Text('暂无内容'))),
                ],
              )
            : ListView.builder(
                padding: const EdgeInsets.all(10),
                itemCount: logic.events.length,
                itemBuilder: (context, index) {
                  final event = logic.events[index];
                  return EventCard(
                    title: event.title,
                    startTime: event.startTime,
                    taskDetail: event.taskDetail,
                    isCompleted: event.isCompleted,
                    onTap: () => logic.toggleEventStatus(index),
                    onDelete: () => logic.removeEvent(index),
                  );
                },
              ),
      ),
    );
  }
}
